using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using OpenArrow.Templating.Parsing;
using OpenArrow.Templating.Compilation;

namespace OpenArrow.Templating.Processing
{
    /// <summary>
    /// Represents the result of a processing action
    /// </summary>
    public class ProcessorResult
    {
        #region Private Fields

        private CompilerResults codeCompilerResults;
        private TemplateParserResults templateParserResults;
        private TemplateCompilerResults templateCompilerResults;
        private ProblemCollection allProblems = null;

        #endregion

        #region Public Properties

        public CompilerResults CodeCompilerResults
        {
            get { return codeCompilerResults; }
            set { codeCompilerResults = value; }
        }

        public TemplateParserResults TemplateParserResults
        {
            get { return templateParserResults; }
            set { templateParserResults = value; }
        }

        public TemplateCompilerResults TemplateCompilerResults
        {
            get { return templateCompilerResults; }
            set { templateCompilerResults = value; }
        }

        public ProblemCollection AllProblems
        {
            get
            {
                if (allProblems == null)
                {
                    allProblems = new ProblemCollection();
                    allProblems.AddRange(templateParserResults.Problems);
                    allProblems.AddRange(templateCompilerResults.Problems);
                    ConvertCompilerErrors(codeCompilerResults.Errors, allProblems);
                }
                return allProblems;
            }
        }

        #endregion

        #region Public Constructors

        public ProcessorResult(CompilerResults codeCompilerResults, TemplateParserResults templateParserResults,
            TemplateCompilerResults templateCompilerResults)
        {
            this.CodeCompilerResults = codeCompilerResults;
            this.TemplateParserResults = templateParserResults;
            this.TemplateCompilerResults = templateCompilerResults;
        }

        #endregion

        #region Private Methods

        private static void ConvertCompilerErrors(CompilerErrorCollection errors, ProblemCollection problems)
        {
            foreach (CompilerError error in errors)
            {
                if (error.IsWarning)
                    problems.AddWarning(error.ErrorText, error.Line, error.Column, error.ErrorNumber);
                else
                    problems.AddError(error.ErrorText, error.Line, error.Column, error.ErrorNumber);
            }
        }

        #endregion
    }
}
